package com.sxj.corejava.code09_集合.Map集合;

import java.util.*;
import java.util.Map.Entry;

/**
 * @author 石小俊
 * @date 2024年05月07日 19:44
 */
public class Test02_遍历方式 {

    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "admin");
        map.put(2, "lily");
        map.put(3, "jack");
        map.put(4, "tom");
        map.put(5, "alice");
//        test01(map);
//        test02(map);
        test03(map);
    }


    public static void test01(Map<Integer, String> map) {
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            System.out.println("key:" + key + ",value:" + map.get(key));
        }
    }

    public static void test02(Map<Integer, String> map) {
        Collection<String> values = map.values();
        for (String s : values) {
            System.out.println(s);
        }
    }

    public static void test03(Map<Integer, String> map) {
//        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        Set<Entry<Integer, String>> entrySet = map.entrySet();
        Iterator<Entry<Integer, String>> it = entrySet.iterator();
        while (it.hasNext()) {
            Entry<Integer, String> entry = it.next();
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println("key:" + key + ",value:" + value);
        }
    }

}
